/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.enums.Status;
import br.com.foxline.model.*;
import br.com.foxline.service.*;
import br.com.foxline.util.Msg;
import java.io.Serializable;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerReserve implements Serializable {

    private Enterprise enterprise;
    private Lot lot;
    private Integer block;
    @EJB
    private EnterpriseService enterpriseService;
    @EJB
    private LotService lotService;
    @EJB
    private MessageService messageService;
    @EJB
    private UserService userService;
    @EJB
    private CorrectorService correctorService;
    @EJB
    private ReserveService reserveService;
    private Reserve reserve;
    private Corrector corrector;
    private boolean isCorrector = true;
    private boolean usesCurrentUser = false;
    private int days = 1;
    
    @PostConstruct
    public void init() {
        this.enterprise = new Enterprise();
        this.lot = new Lot();
        this.corrector = new Corrector();
        this.reserve = new Reserve();
        this.days = 1;
        Corrector c = null;
        try {
            c = correctorService.findCorrectorByUser(userService.getCurrentUser());
        } catch (Exception e) {
        }
        if (c == null) {
            isCorrector = false;
        } else {
            usesCurrentUser = true;
            this.corrector = c;
        }

    }
    
    public void reserve(){  
        reserve.setEnterprise(enterprise);
        reserve.setLot(lot);
        this.lot.setStatus(Status.RESERVADO);
        reserve.setReserverCorrector(corrector);
        reserve.setResponsibleUsers(userService.getCurrentUserFull());
        reserve.setReservedDays(days);
        reserveService.save(reserve);
        this.lotService.update(lot);
        Msg.messagemInfo("Lote reservado");
        init();
    }
    
    
    public void loadCorrector() {
        if (usesCurrentUser) {
            this.corrector = correctorService.findCorrectorByUser(this.userService.getCurrentUser());
        } else {
            this.corrector = new Corrector();
        }
    }

    public List<Enterprise> autocompleteEnterprise(String query) {
        return enterpriseService.autocomplete(query);
    }

    public List<Corrector> autocompleteCorrector(String query) {
        return correctorService.autocomplete(query);
    }

    public List<SelectItem> lotsByEnterpriseAndBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        List<Lot> lots = lotService.lotsDisponiveis(enterprise, block);
        for (Lot lot : lots) {
            items.add(new SelectItem(lot.getId(), "Lote " + lot.getNum().toString()));
        }
        return items;
    }

    public List<SelectItem> selectTypeBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();

        if (enterprise != null && enterprise.getQtdBlock() != null) {
            for (int i = 1; i <= enterprise.getQtdBlock(); i++) {
                items.add(new SelectItem(i, "Quadra " + i));
            }
        }
        return items;
    }

    public void loadLot() {
        this.lot = lotService.find(lot.getId());
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public Integer getBlock() {
        return block;
    }

    public void setBlock(Integer block) {
        this.block = block;
    }

    public Lot getLot() {
        return lot;
    }

    public void setLot(Lot lot) {
        this.lot = lot;
    }

    public Corrector getCorrector() {
        return corrector;
    }

    public void setCorrector(Corrector corrector) {
        this.corrector = corrector;
    }

    public boolean isIsCorrector() {
        return isCorrector;
    }

    public void setIsCorrector(boolean isCorrector) {
        this.isCorrector = isCorrector;
    }

    public boolean isUsesCurrentUser() {
        return usesCurrentUser;
    }

    public void setUsesCurrentUser(boolean usesCurrentUser) {
        this.usesCurrentUser = usesCurrentUser;
    }

    public int getDays() {
        return days;
    }

    public void setDays(int days) {
        this.days = days;
    }
    
    
    public String dimensaoDoLote(){
        if(lot == null || lot.getId() == null){
            return "";
        }
        System.out.println(lot);
        if(lot.getDimessionX() != null && lot.getDimessionY()!= null){
            return lot.getDimessionX() + " X " + lot.getDimessionY();
        }else{
            return "VARIÁVEL";
        }
    }
    
    public void expireReserve() {
        //reserveService.expireReserve();
    }
    
}
